using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using Google.Protobuf;
using QQ.Client;
using QQ.Packet.Response;
using System.Collections.Generic;

namespace QQ.Codecs
{
    public class ProtobufDecoder : MessageToMessageDecoder<SSOFrame>
    {
        public override bool IsSharable => true;

        private readonly LoginSigInfo loginSigInfo;
        private readonly ClientCallback clientCallback;
        private Dictionary<string, MessageParser> protoMap = new Dictionary<string, MessageParser>()
        {
            {"OnlinePush.PbPushGroupMsg",Qqprotobuf.OnlinePush.PbPushMsg.Parser}
        };
        //use attribute and reflection to auto genenrate this.

        public ProtobufDecoder(ClientCallback clientCallback, LoginSigInfo loginSigInfo)
        {
            this.loginSigInfo = loginSigInfo;
            this.clientCallback = clientCallback;
        }

        protected override void Decode(IChannelHandlerContext context, SSOFrame frame, List<object> output)
        {
            if (protoMap.ContainsKey(frame.commandName))
            {
                IMessage message = protoMap[frame.commandName].ParseFrom(frame.plainBody.ToArray());
                switch (message)
                {
                    case Qqprotobuf.OnlinePush.PbPushMsg msg: clientCallback.PbPushMsg_callback(msg); break;
                }
            }
            else output.Add(frame);//write to parse next
        }
    }
}
